package com.julang.check.controller.alipay;

import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.jpay.alipay.AliPayApiConfig;
import com.julang.check.exception.ErrorTokenException;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
/**
 * @author 赵自强
 * */
public abstract class AbstractAliPayApiController {
	/**
	 * 阿里巴巴支付配置
	 * @return AliPayApiConfig
	 */
	public abstract AliPayApiConfig getApiConfig();

	public HttpServletRequest getRequest() {
		RequestAttributes ra = RequestContextHolder.getRequestAttributes();
		return ((ServletRequestAttributes) ra).getRequest();
	}

	public String getUserId() {
		String token = this.getRequest().getHeader("Authorization");
		String userId = null;
		try {
			userId = JWT.decode(token).getAudience().get(0);
		} catch (JWTDecodeException j) {
			throw new ErrorTokenException(token);
		}
		return userId;
	}
}